package cn.tsmchina.one.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.Return;

public class SpringLoginInterceptor implements HandlerInterceptor {
	
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,
		Object handler) throws Exception {
		String path = request.getServletPath();
		
		//登录：要放行
		if(path.startsWith("/login")){
			return true;
		}
		
		//轮询命令，麻烦放行
		if(path.startsWith("/order/pull")){
			return true;
		}
		
		
//		if(path.startsWith("/order/info")) {
//			return true;
//		}
				
		//已经登录，要放行
		else if(request.getSession().getAttribute("customer")==null)
		{
			response.sendRedirect(request.getContextPath()+"/msg?msgType=ok&msg="+Return.encode("请登录!")+"&return=login");
			return false;
		}
		
		//已经登陆，查看订单放行
		else if(path.startsWith("/order/query")) {
		return true;
	}
		//已经登陆，且为超级管理员，放行
		else if(null != LoginUtil.getCustomerType(request) && LoginUtil.getCustomerType(request).intValue()==1)
		{
			return true;
		}
		//其他情况非法
		else{
			response.sendRedirect(request.getContextPath()+"/msg?msgType=ok&msg="+Return.encode("you are not admin!")+"&return=login");
			return false;
		}
	}  
	
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
